﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WanliMDC.SpiService.Repositories
 * 唯一标识：de962fad-e48f-4b7d-afa9-67da3c6db710
 * 文件名：BWmsEquipErrs
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/6/2 14:54:16
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BWmsEquipErrs : IWmsEquipErrs
{
    public int AddEquipErr(long equipId, int alarmNo)
    {
        var db = Db.Default;
        // 添加当前报警数据
        var result = db.WmsEquipErrs.Insert(new POWmsEquipErr
        {
            Id = XID.Nextval,
            EquipId = equipId,
            ErrCode = alarmNo,
            ErrMsg = "",
            BeginTime = DateTime.Now,
        });
        return result;
    }
    public int AddEquipErr(long equipId, int alarmNo, string alarmCode, string alarmMsg)
    {
        var db = Db.Default;
        // 添加当前报警数据
        var result = db.WmsEquipErrs.Insert(new POWmsEquipErr
        {
            Id = XID.Nextval,
            EquipId = equipId,
            ErrCode = alarmNo,
            ErrCodeArr = alarmCode,
            ErrMsg = alarmMsg,
            BeginTime = DateTime.Now,
        });
        return result;
    }

    public int AddEquipErrV2(long equipId, int alarmNo, string alarmCode, string alarmMsg, int? taskNo)
    {
        var db = Db.Default;
        // 添加当前报警数据
        var result = db.WmsEquipErrs.Insert(new POWmsEquipErr
        {
            Id = XID.Nextval,
            EquipId = equipId,
            ErrCode = alarmNo,
            ErrCodeArr = alarmCode,
            ErrMsg = alarmMsg,
            BeginTime = DateTime.Now,
            TaskNo = taskNo
        });
        return result;
    }
    public int AddEquipErrV3(long equipId, int alarmNo, string alarmCode, string alarmMsg, int? taskNo, int? taskNo2)
    {
        var db = Db.Default;
        // 添加当前报警数据
        var result = db.WmsEquipErrs.Insert(new POWmsEquipErr
        {
            Id = XID.Nextval,
            EquipId = equipId,
            ErrCode = alarmNo,
            ErrCodeArr = alarmCode,
            ErrMsg = alarmMsg,
            BeginTime = DateTime.Now,
            TaskNo = taskNo,
            TaskNo2 = taskNo2
        });
        return result;
    }



    public POWmsEquipErr GetNotFinishEquipErr(long equipId, int alarmNo)
    {
        var db = Db.Default;
        var result = db.WmsEquipErrs.Where(t => t.EquipId == equipId && t.EndTime == null).FirstOrDefault();
        return result;
    }

    public int UpdateFinishEquipErr(long equipId)
    {
        var db = Db.Default;
        var errList = db.WmsEquipErrs.Where(t => t.EquipId == equipId && t.BeginTime != null && t.EndTime == null).ToList();
        errList.ForEach(item =>
        {
            item.EndTime = DateTime.Now;
        });

        var num = db.SaveChanges();
        return num;
    }
}
